﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PartyInvites.Models.Product.Extension
{
    public static class ProductExtension
    {
        public static decimal GetTotal(this ShoppingCart cart)
        {
            decimal total = 0;
            foreach (var item in cart.Products)
            {
                total += item.Price;
            }
            return total;
        }

        public static IEnumerable<ProductObj> FilterByCategory(this IEnumerable<ProductObj> products,string categoryParam)
        {
            foreach (var prod in products)
            {
                if (categoryParam == "1")
                {
                    yield return prod;
                }
            }
        }

        public static IEnumerable<ProductObj> Filter(this IEnumerable<ProductObj> products,Func<ProductObj,bool> selectorParam)
        {
            foreach (var productObj in products)
            {
                if (selectorParam(productObj))
                {
                    yield return productObj;
                }
            }
        }

        public static decimal GetTotal(this IEnumerable<ProductObj> products)
        {
            decimal total = 0;
            foreach (var item in products)
            {
                total += item.Price;
            }
            return total;
        }

        public static decimal GetTotalByType(this IEnumerable<ProductObj> products,string type)
        {
            decimal total = 0;
            foreach (var item in products)
            {
                if (item.Type == type)
                {
                    total += item.Price;
                }
            }
            return total;
        }

        public static decimal GetTotalByFilter(this IEnumerable<ProductObj> products, Func<ProductObj,bool> selectorParam)
        {
            decimal total = 0;
            foreach (var item in products)
            {
                if (selectorParam(item))
                {
                    total += item.Price;
                }
            }
            return total;
        }
    }
}